#! /bin/sh


###################
## configuration ##
###################


# NOTE: If I set this in Xresources other apps work
# For example Rofi ..if I set here XResources is ignored ..not sure
# ALSO trying to use logic if/then for when I use different monitors seems to
# crash if I use it..however one liner works ..very odd.
#echo "Xft.dpi: 120" | xrdb + - &&
# apply a scaling for 4k internal monitor
#echo "Xft.dpi: 192" | xrdb + - &&

# set Project Glass for Java Apps
wmname LG3D

# This sets the intel internal laptop res/scale, lets me keep everything the
# same when an external laptop is connect.  Presumably it will error
# when nvidia is connect which is used for external monitor on this laptop
#xrandr --output eDP-1 --dpi 276 --scale 1x1 --mode 2880x1620 --fb 3840x2160
# This setting below is closest to the *best* scaling macbook does by default
xrandr --output eDP-1 --dpi 276 --scale 1x1 --mode 2048x1152 --fb 3840x2160

# If you use fedora/gnome default mask set is 0022, for some reason
# in bspwm it's 0077 which okay except when using docker which screws things
# up when you add files to the image.  It retains the mask.  Might run
# into perm issues when non-root UID is used to run things because there no
# a+rx on those items due to 0077 for new files on the host
umask 0022

# bspwm configuration

bspc config border_width 2 
bspc config window_gap 12
bspc config top_padding 18
bspc config bottom_padding 0 
bspc config right_padding 0
bspc config left_padding 0


bspc config split_ratio 0.52
bspc config borderless_monocle true
bspc config gapless_monocle true
#bspc config paddingless_monocle true


bspc config remove_disabled_monitors true
bspc config remove_unplugged_monitors true
#bspc config merge_overlapping_monitors true
# --- bspc subscribe monitor_remove --- Becareful of this one

bspc config focus_by_distance true
bspc config focus_follows_pointer true
bspc config click_to_focus false
bspc config pointer_modifier super
bspc config pointer_action1 move
bspc config pointer_action2 resize_side
bspc config pointer_action3 resize_corner

#bspc config pointer_modifier mod1
#bspc config pointer_action1 move
#bspc config pointer_action2 resize_side
#bspc config pointer_action3 resize_corner



# Setup the screens ..laptop/external maybe FUTURE multiple screens special.
# WIP
#if [ -x ~/.config/bspwm/scripts/monitor_setup ];then
#    scripts/monitor_setup
#fi
#xrandr --output eDP-1-1 --set "PRIME Synchronization" 1
#xrandr --setprovideroutputsource modesetting NVIDIA-0
#randr --auto

# workspaces
bspc monitor -d web term code chat music
#if [[ $(hostname) = moria ]]; then
    #if [[ $(cat /proc/acpi/button/lid/LID/state | grep -c "closed") -gt 0 ]]; then
        # Means I started with lid closed and I dont want my desktop to be shown
        # on there
    #    bspc monitor DP-2 --remove
    #fi
    #bspc monitor HDMI-0 -d web term code chat music
#else
    # Unknown host so lets just create a default desktop on each monitor
#    for monitor in $(xrandr -q | grep " connected" | cut -d ' ' -f1); do
#        bspc monitor $monitor -d 1
#    done
#fi

## color settings
bspc config focused_border_color "#d8dee8"
bspc config normal_border_color "#2f343f"
bspc config active_border_color "#2f343f"


##################
## window rules ##
##################

bspc rule -a Gimp desktop='^8' state=floating follow=on
bspc rule -a Chromium desktop='^1'
bspc rule -a Firefox desktop='web'
bspc rule -a mplayer2 state=floating
bspc rule -a Kupfer.py focus=on
bspc rule -a Screenkey manage=off
bspc rule -a jetbrains-idea desktop='code'
bspc rule -a jetbrains-pycharm desktop='code'
bspc rule -a Slack state=floating desktop='chat'

##############
## programs ##
##############
# bitmap fonts
xset +fp /home/vooi/.local/share/fonts &
xset fp rehash &

# compton
compton --config ~/.config/compton/beta.conf -b &

# volume icon
volumeicon &

# keyboard bindings manager
SXHKD_SHELL='/usr/bin/sh' sxhkd &

# network applet -- need trayicon support for this
nm-applet &

# music daemon
# mpd &

# notification manager
dunst &

# set the wallpaper
feh --bg-center ~/Pictures/mountain-forest.jpg &

# cursor theme
xsetroot -cursor_name left_ptr &

# Panel
#if [[ $(cat /proc/acpi/button/lid/LID/state | cut -d ':' -f2 | tr  -d ' ') = closed ]]; then
    #MONITOR=HDMI-0 polybar bspwm-bar &
#elif
#polybar bspwm-bar &
#if


# This handles creating polybar on multiple monitors
MONITOR_LIST=$(xrandr --listmonitors | sed '1 d' | awk 'NF>1{print $NF}')

killall polybar 2> /dev/null

for mon in $MONITOR_LIST; do
    echo "Launching on monitor $mon"
    env MONITOR=$mon polybar -r bspwm-bar &
done

redshift &

bspc wm --adopt-orphans

xautolock -time 10 -locker lock &
# belw is a work in progress
#xset s 120 180
#xss-lock -l -n  -- ${HOME}/.config/bspwm/scripts/lock
